#include <stdio.h>

int main(int argc, char const *argv[])
{
    FILE *pipe_fp, *infile;
    char readbuf[80];

    if(argc != 3) {
        fprintf(stderr, "USAGE: popen3 [command] [filename]\n");
        exit(1);
    }

    if(( infile = fopen(argv[2], "rt")) == NULL) { // 打开输入文件
        perror("fopen");
        exit(1);
    }

    if(( pipe_fp = popen(argv[1], "w")) == NULL) { // 建立写管道
        perror("popen");
        exit(1);
    }

    do
    {
        fgets(readbuf, 80, infile); // 读infile文件中的内容
        if(feof(infile)) break;

        fputs(readbuf, pipe_fp); // 写入管道
    } while (!feof(infile));
    
    fclose(infile);
    pclose(pipe_fp);

    return 0;
}
